package coint.btc.config.jdbc;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

import lombok.extern.log4j.Log4j;

/**
 * jdbc druid连接配置
 * @ClassName DruidConfig
 * @author BOOM
 * @date 2018/05/18
 *
 */
@Configuration
@Log4j
public class DruidConfig {
	
	private Logger log=LoggerFactory.getLogger(DruidConfig.class);

	@Value("${spring.druid.datasource.type}")
	private String type;
	@Value("${spring.druid.datasource.driverClassName}")
	private String driverClassName;
	@Value("${spring.druid.datasource.url}")
	private String url;
	@Value("${spring.druid.datasource.username}")
	private String username;
	@Value("${spring.druid.datasource.password}")
	private String password;
	@Value("${spring.druid.datasource.initialSize}")
	private Integer initialSize;
	@Value("${spring.druid.datasource.minIdle}")
	private Integer minIdle;
	@Value("${spring.druid.datasource.maxActive}")
	private Integer maxActive;
	@Value("${spring.druid.datasource.maxWait}")
	private Long maxWait;
	@Value("${spring.druid.datasource.timeBetweenEvictionRunsMillis}")
	private Long timeBetweenEvictionRunsMillis;
	@Value("${spring.druid.datasource.minEvictableIdleTimeMillis}")
	private Long minEvictableIdleTimeMillis;
	@Value("${spring.druid.datasource.validationQuery}")
	private String validationQuery;
	@Value("${spring.druid.datasource.testWhileIdle}")
	private Boolean testWhileIdle;
	@Value("${spring.druid.datasource.testOnBorrow}")
	private Boolean testOnBorrow;
	@Value("${spring.druid.datasource.testOnReturn}")
	private Boolean testOnReturn;
	@Value("${spring.druid.datasource.poolPreparedStatements}")
	private Boolean poolPreparedStatements;
	@Value("${spring.druid.datasource.maxPoolPreparedStatementPerConnectionSize}")
	private Integer maxPoolPreparedStatementPerConnectionSize;
	@Value("${spring.druid.datasource.filters}")
	private String filters;
	@Value("${spring.druid.datasource.connectionProperties}")
	private String connectionProperties;
	
	@Value("${redis.host}")
	private String redisHost;
	@Value("${redis.port}")
	private String redisPort;
	@Value("${redis.password}")
	private String redisPwd;
	
	@Bean
    @Primary
    public DataSource dataSource(){  
        DruidDataSource datasource = new DruidDataSource();  
        datasource.setUrl(this.url);  
        datasource.setUsername(username);  
        datasource.setPassword(password);  
        datasource.setDriverClassName(driverClassName);  
        datasource.setInitialSize(initialSize);  
        datasource.setMinIdle(minIdle);  
        datasource.setMaxActive(maxActive);  
        datasource.setMaxWait(maxWait);  
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        datasource.setValidationQuery(validationQuery);  
        datasource.setTestWhileIdle(testWhileIdle);  
        datasource.setTestOnBorrow(testOnBorrow);  
        datasource.setTestOnReturn(testOnReturn);  
        datasource.setPoolPreparedStatements(poolPreparedStatements);  
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  
        try {  
            datasource.setFilters(filters);  
        } catch (SQLException e) {  
            log.error("数据库连接异常", e);
            
        }  
        datasource.setConnectionProperties(connectionProperties);  
          
        return datasource;  
    }

	public String getRedisHost() {
		return redisHost;
	}

	public String getRedisPort() {
		return redisPort;
	}

	public String getRedisPwd() {
		return redisPwd;
	} 
	
	
}
